package tomasulo.ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.EventListenerList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class UITable {
	
	private int nRow = 0;
	private int nColumn = 0;
	private JTable table = null;
	private JScrollPane scrollPane = null;
	private JPanel panel = null;
	private JLabel label = null;
	private Vector<Vector<String>> cell = null;
	private DefaultTableModel model = null;
	private String title[] = null;
	
	private Vector<String> getNewLine() {
		Vector<String> ret = new Vector<String>(nColumn);
		for (int i = 0; i < nColumn; ++i) {
			ret.add(new String(""));
		}
		return ret;
	}
	
	public void addTableModelListener(TableModelListener listener) {
		table.getModel().addTableModelListener(listener);
	}
	
	public UITable(
			int row, 
			int column, 
			final String columnTitle[], 
			final UITableEditable editable) {
		nRow = row;
		nColumn = column;
		label = new JLabel();
		label.setFont(new Font("", Font.BOLD, 15));
		panel = new JPanel(new BorderLayout());
		cell = new Vector<Vector<String>>(row);
		for (int i = 0; i < row; ++i) {
			cell.add(getNewLine());
		}
		title = columnTitle;
		model = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;
			@Override
			public int getRowCount() {
				return nRow;
			}
			@Override
			public int getColumnCount() {
				return nColumn;
			}
			@Override
			public String getColumnName(int column) {
				return title[column];
			}
			@Override
			public boolean isCellEditable(int row, int column) {
				return editable.isEditable(row, column);
			}
			@Override
			public String getValueAt(int row, int column) {
				return cell.get(row).get(column);
			}
			@Override
			public void setValueAt(Object value, int row, int column) {
				cell.get(row).set(column, (String)value);
				Object[] ls = super.listenerList.getListenerList();
				for (int i = ls.length - 2; i >= 0; i -= 2) {
					if (ls[i] == TableModelListener.class) {
						TableModelEvent e = new TableModelEvent(this, row, row, column);
						((TableModelListener)ls[i + 1]).tableChanged(e);
					}
				}
			}
		};
		table = new JTable(model);
		table.setShowGrid(true);
		table.setFillsViewportHeight(true);
		scrollPane = new JScrollPane(table);
		panel.add(label, BorderLayout.NORTH);
		panel.add(scrollPane, BorderLayout.CENTER);
	}
		
	public void setAt(int row, int column, String value) {
		if (column >= nColumn) {
			return;
		}
		cell.ensureCapacity(row + 1);
		while (row >= nRow) {
			cell.add(getNewLine());
			++nRow;
		}
		table.getModel().setValueAt(value, row, column);
	}
	
	public void clear() {
		for (int i = 0; i < cell.size(); ++i) {
			if (cell.get(i) == null) break;
			for (int j = 0; j < cell.get(i).size(); ++j) {
				setAt(i, j, "");
			}
		}
	}
	
	public String getAt(int row, int column) {
		return cell.get(row).get(column);
	}
	
	public void setTableTitle(String title) {
		label.setText(title);
	}
	
	public JPanel getTablePane() {
		return panel;
	}
	
}
